package gjw.jimven.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.List;

import gjw.jimven.R;
import gjw.jimven.bean.DataBean;

/**
 * @创建者 Jimven
 * @创建时间 2017/8/25/0025  10:14
 */
public class MyAdapter extends BaseAdapter {
    private Context mContext;

    private List<DataBean> mDatas;

    private LayoutInflater mInflater;

    public boolean flage = false;

    public MyAdapter(Context mContext, List<DataBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_item_data, null);
            holder.checkboxOperateData = (CheckBox) convertView.findViewById(R.id.checkbox_operate_data);
            holder.textTitle = (TextView) convertView.findViewById(R.id.text_title);
            holder.textDesc = (TextView) convertView.findViewById(R.id.text_desc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            final DataBean dataBean = mDatas.get(position);
            if (dataBean != null) {
                holder.textTitle.setText(dataBean.title);
                holder.textDesc.setText(dataBean.desc);
                // 根据isSelected来设置checkbox的显示状况
                if (flage) {
                    holder.checkboxOperateData.setVisibility(View.VISIBLE);
                } else {
                    holder.checkboxOperateData.setVisibility(View.GONE);
                }
                holder.checkboxOperateData.setChecked(dataBean.isCheck);
                holder.checkboxOperateData.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (dataBean.isCheck) {
                            dataBean.isCheck = false;
                        } else {
                            dataBean.isCheck = true;
                        }
                    }
                });
            }
        }
        return convertView;
    }

    class ViewHolder {
        CheckBox checkboxOperateData;
        TextView textTitle;
        TextView textDesc;
    }
}
